package generation.makers;

import generation.scale.Scale;
import generation.scale.ScaleFactory;

import java.io.File;
import java.io.IOException;

import org.jfugue.IntervalNotation;
import org.jfugue.Pattern;
import org.jfugue.Player;

import util.ConfigFile;
import util.Utils;

public class SongMaker {

	protected Scale scale;
	
	public SongMaker(Scale scale){
		this.scale = scale;
	}

	public String generateSong(int compassQuant){
		String chords = "";
		String fourCompass = "";
		for (int i = 0; i < compassQuant; i++) {
			String currentChords = HarmonyMakerImpl.generateChords(this.scale);
			String[] currentChordsArray = Utils.getArrayFromString(currentChords, " ");
			String[] chordsBeat = DurationMakerImpl.generateChordsBeat();
			String chordsWithBeat = mergeWithBeat(currentChordsArray, chordsBeat);
			chords = chords + " " + chordsWithBeat;
			for (int j = 0; j < 4; j++) {
				String dominantTune = HarmonyMakerImpl.getTuneRelChord(currentChordsArray[j]);
				fourCompass = fourCompass + this.generateCompass(dominantTune) + " | ";
			}
		}		
		chords = "V0 " + ConfigFile.getString("instrument.chord") + " " + chords;
		fourCompass = "V1 " + ConfigFile.getString("instrument.melody") + " " + fourCompass;
		return chords + " " + fourCompass;
	}

	protected String generateCompass(String initialTune){
		String compass = "";
		String[] beat = DurationMakerImpl.generateFourLengthBeat();
		String[] melody = MelodyMakerImpl.generateMelody(beat.length, initialTune, this.scale);
		String mergedMelody = this.mergeWithBeat(melody, beat);
		compass = compass + mergedMelody;
		return compass;
	}

	protected String mergeWithBeat(String[] passes, String[] beat){
		String mergedWithBeats = "";
		for (int i = 0; i < beat.length; i++) {
			mergedWithBeats = mergedWithBeats + " " + passes[i] + beat[i];
		}
		return mergedWithBeats;
	}

	public static void saveMusic(Pattern pattern, Player player, String scaleName, String rootNote){
		String timeStamp = Utils.getTimeStamp();
		try {
			Utils.copyFile("src/config.properties", "midi/" + timeStamp + rootNote + scaleName  + ".pro");
			pattern.savePattern(new File("midi/" + timeStamp + rootNote + scaleName + ".txt"));
			player.saveMidi(pattern, new File("midi/" + timeStamp + rootNote + scaleName + ".mid"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Pattern giveMeAPattern(int numCompass){
		String scaleName = "pentmin";//ConfigFile.getString("scale.name");
		String rootNote = "C5";//ConfigFile.getString("scale.rootNote");
		//int compassQuant = ConfigFile.getInt("song.numCompass");
		Scale scale = ScaleFactory.getScale(scaleName);
		SongMaker m = new SongMaker(scale);
		String song = m.generateSong(numCompass);
		System.out.println(song);
		IntervalNotation in = new IntervalNotation(song);
		Pattern pattern = in.getPatternForRootNote(rootNote);
		//Player player = new Player();
		//SongMaker.saveMusic(pattern, player, scaleName, rootNote);
		System.out.println(pattern.getMusicString());
		//player.play(pattern);
		return pattern;
	}
	
	public static void main(String[] args) {
//		String scaleName = ConfigFile.getString("scale.name");
//		String rootNote = ConfigFile.getString("scale.rootNote");
//		int compassQuant = ConfigFile.getInt("song.numCompass");
//		Scale scale = ScaleFactory.getScale(scaleName);
//		SongMaker m = new SongMaker(scale);
//		String song = m.generateSong(compassQuant);
//		System.out.println(song);
//		IntervalNotation in = new IntervalNotation(song);
//		Pattern pattern = in.getPatternForRootNote(rootNote);
//		SongMaker.saveMusic(pattern, player, scaleName, rootNote);
//		System.out.println(pattern.getMusicString());
		// The next line activates the twitter acquisition
//		final Vector<Vector<String>> stringsParaCantar = Gainsbug.tweetizer("love");
//		final Vector<Vector<String>> stringsParaCantar2 = Gainsbug.tweetizer("song");
//		final Vector<Vector<String>> stringsParaCantar3 = Gainsbug.tweetizer("battle");
//	
//		Vector<Vector<String>> merged = Gainsbug.mixerAndMerger(stringsParaCantar,
//				stringsParaCantar2, stringsParaCantar3);
//		
//		Player player = new Player();
//		Pattern pattern = new Pattern(SongMaker.generateSongFromTwitter(merged));
//		Pattern pattern = giveMeAPatternFromTwitter(merged);
//		player.play(pattern);
	}

}
